/**
 *  Copyright(C) 2007 Juri Strumpflohner
 *  All rights reserved
 *
 *  Created: 04.08.2007
*/

package org.aroundme.sandbox;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.aroundme.model.Search;
import org.aroundme.utility.SearchTokenizer;

public class TokenizeTest {
//	public static String search = "juri OR thomas restaurant place OR cat OR house";
	public static String search = "juri and tom restaurant OR computer OR cat OR house";
	public static void main(String[] args) {
//		WHITESPACEPattern();
//		GLOBALOR();
//		ORPattern();
//		AND();
		SearchTokenizer token = new SearchTokenizer();
		Search s = token.createSearchObject(search);
		System.out.println("AND keywords: ");
		for (String string : s.getKeywordsAND()) {
			System.out.println(" >> " + string);
		}
		System.out.println("or keywords: ");
		for (String string : s.getKeywordsOR()) {
			System.out.println(" >> " + string);
		}
	}

	private static void WHITESPACEPattern() {
		final String REGEX_WHITESPACE = "[\\s][\\s]+";
		search = search.replaceAll(REGEX_WHITESPACE, " ");	
	}

	private static void GLOBALOR(){
		final String REGEX_OR_CHAIN = "[\\w]+\\sOR\\s[\\w]+[\\w]+\\sOR\\s[\\w]+[\\sOR\\s[\\w]+]*";
		Pattern pattern = Pattern.compile(REGEX_OR_CHAIN);
		Matcher matcher = pattern.matcher(search);
		
		while(matcher.find()){
			String substring = search.substring(matcher.start(), matcher.end());
			System.out.println(substring);
//			String[] splitter = substring.split(" OR ");
//			for (String string : splitter) {
//				System.out.println(string);
//			}
//			search = search.substring(0, matcher.start()) + search.substring(matcher.end(), search.length());
//			System.out.println("Transformed string:" + search);
		}	
	}
	
	private static void ORPattern() {
		final String REGEX_OR_COUPLE = "[\\w]+\\s+OR\\s+[\\w]+";
		
		
		Pattern pattern = Pattern.compile(REGEX_OR_COUPLE);
		Matcher matcher = pattern.matcher(search);
		
		while(matcher.find()){
			String substring = search.substring(matcher.start(), matcher.end());
			System.out.println(substring);
//			String[] splitter = substring.split(" or ");
//			for (String string : splitter) {
//				System.out.println("Couple or: " + string);
//			}
//			//remove from search-string
//			search = search.substring(0, matcher.start()) + search.substring(matcher.end(), search.length());
//			System.out.println("Transformed search string: " + search);
		}
	}
	
	private static void AND(){
		final String REGEX_WORD = "[\\w]+";
		
//		search = search.replaceAll("\\s", "*");
		Pattern pattern = Pattern.compile(REGEX_WORD);
		Matcher matcher = pattern.matcher(search);
		
		while(matcher.find()){
			String substring = search.substring(matcher.start(), matcher.end());
			System.out.println(substring);
		}
	}
}
